package com.hera.process.config.rest.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hera.base.domain.MensajeJSON;
import com.hera.base.rest.controller.BaseController;
import com.hera.base.util.FiltroDTO;
import com.hera.process.config.service.AddTableDetailService;
import com.hera.process.config.service.AddTableIndexService;
import com.hera.process.config.service.AdditionalService;
import com.hera.process.config.service.MetadataManagerService;
import com.hera.process.external.domain.AddTableDetail;
import com.hera.process.external.domain.AddTableIndex;
import com.hera.process.workflow.domain.Stage;

@Controller
@RequestMapping("external")
public class ExternalController extends BaseController {

	static final Logger logger = Logger.getLogger(ExternalController.class);
	
	@Autowired
	private AdditionalService additionaltableservice;
	
	@Autowired
	private AddTableDetailService addtabledetailservice;
	
	@Autowired
	private AddTableIndexService addtableindexservice;
	
	@Autowired
	private MetadataManagerService metadataManagerService;
	
	
	/*
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////// TASKUSER SERVICE ////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 */

	@RequestMapping(value = "adddetail/list", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON lisAddTableDetails(@RequestBody FiltroDTO filtroDTO) {
		logger.info("listSecuences  " + filtroDTO.toString());
		
		return obtenerMensajeJSON(addtabledetailservice.findAll(filtroDTO));
   }

	@RequestMapping(value = "adddetail/save", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON saveAddTableDetail(@RequestBody AddTableDetail adddetail) {
		logger.info("grabando el parametro....");
		addtabledetailservice.save(adddetail);
		return obtenerMensajeJSON(adddetail);
   }
	
	@RequestMapping(value = "adddetail/update", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON updateAddTableDetail(@RequestBody AddTableDetail adddetail) {
		logger.info("grabando el parametro....");
		addtabledetailservice.update(adddetail);
		return obtenerMensajeJSON(adddetail);
   }
	
	@RequestMapping(value = "adddetail/delete", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON deleteAddTableDetail(@RequestBody AddTableDetail adddetail) {
		logger.info("grabando el parametro....");
		addtabledetailservice.delete(adddetail);
		return obtenerMensajeJSON(adddetail);
   }
	
	/*
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////// ADDTABLEINDEX SERVICE ////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 */
	
	
	@RequestMapping(value = "addtableindex/list", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON lisAddTableIndexs(@RequestBody FiltroDTO filtroDTO) {
		logger.info("listSecuences  " + filtroDTO.toString());
		
		return obtenerMensajeJSON(addtableindexservice.findAll(filtroDTO));
   }
	
	@RequestMapping(value = "addtableindex/save", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON saveAddTableIndex(@RequestBody AddTableIndex addindex) {
		logger.info("grabando el parametro....");
		addtableindexservice.save(addindex);
		return obtenerMensajeJSON(addindex);
   }
	
	@RequestMapping(value = "addtableindex/update", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON updateAddTableIndex(@RequestBody AddTableIndex addindex) {
		logger.info("grabando el parametro....");
		addtableindexservice.update(addindex);
		return  obtenerMensajeJSON(addindex);
   }
	
	@RequestMapping(value = "addtableindex/delete", method = RequestMethod.POST)
   @ResponseBody
   public MensajeJSON deleteAddTableIndex(@RequestBody AddTableIndex addindex) {
		logger.info("grabando el parametro....");
		addtableindexservice.delete(addindex);
		return  obtenerMensajeJSON(addindex);
   }
	
	@RequestMapping(value = "metadata/insert", method = RequestMethod.POST)
	@ResponseBody
	public MensajeJSON saveMetadata(@RequestBody Map<String, Object> entity) throws JsonGenerationException, JsonMappingException, IOException {
		logger.info("grabando el metadata....");
		if(entity.containsKey("$$stage$$")){
			System.out.println(entity.get("$$stage$$"));
			ObjectMapper mapper = new ObjectMapper();
			String strJSON = mapper.writeValueAsString(entity.get("$$stage$$"));
			Stage stage = mapper.readValue(strJSON, Stage.class);
			System.out.println(stage);
			entity.remove("$$stage$$");
		}
		
		metadataManagerService.insert(entity, "WORKFLOW2", "TBL_ABC");
		return  obtenerMensajeJSON(entity);
	}
	
	@RequestMapping(value = "metadata/selectbyid", method = RequestMethod.POST)
	@ResponseBody
	public MensajeJSON saveMetadata(@RequestBody List<Object> fields){
		logger.info("select by id metadata....");
		Long identity = new Long( fields.get(0).toString());
		fields.remove(0);
		return  obtenerMensajeJSON(metadataManagerService.selectById(identity, fields, "WORKFLOW2", "TBL_ABC"));
	}

}
